package com.hktf.moedokilock.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.hktf.corelib.eventBus.WxPayEvent;
import com.hktf.corelib.utils.LogMds;
import com.hktf.uilib.utils.WxsdkUtil;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.greenrobot.eventbus.EventBus;

import java.lang.ref.WeakReference;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {
    private static String TAG = "MicroMsg.WXEntryActivity";

    private IWXAPI api;
    private MyHandler handler;

    private static class MyHandler extends Handler {
        private final WeakReference<WXPayEntryActivity> wxEntryActivityWeakReference;

        public MyHandler(WXPayEntryActivity wxEntryActivity) {
            wxEntryActivityWeakReference = new WeakReference<WXPayEntryActivity>(wxEntryActivity);
        }

        @Override
        public void handleMessage(Message msg) {
            int tag = msg.what;
            //			switch (tag) {
            //				case NetworkUtil.GET_TOKEN: {
            //					Bundle data = msg.getData();
            //					JSONObject json = null;
            //					try {
            //						json = new JSONObject(data.getString("result"));
            //						String openId, accessToken, refreshToken, scope;
            //						openId = json.getString("openid");
            //						accessToken = json.getString("access_token");
            //						refreshToken = json.getString("refresh_token");
            //						scope = json.getString("scope");
            //						Intent intent = new Intent(wxEntryActivityWeakReference.get(), SendToWXActivity.class);
            //						intent.putExtra("openId", openId);
            //						intent.putExtra("accessToken", accessToken);
            //						intent.putExtra("refreshToken", refreshToken);
            //						intent.putExtra("scope", scope);
            //						wxEntryActivityWeakReference.get().startActivity(intent);
            //					} catch (JSONException e) {
            //						Log.e(TAG, e.getMessage());
            //					}
            //				}
            //			}
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        api = WXAPIFactory.createWXAPI(this, WxsdkUtil.APP_ID, false);
//        api.registerApp(appId);
        handler = new MyHandler(this);


        try {
            Intent intent = getIntent();
            api.handleIntent(intent, this);
        } catch (Exception e) {
            e.printStackTrace();
        }
        LogMds.w("cdz","==============WXEntryActivity onCreate=================");

//        final SendAuth.Req req = new SendAuth.Req();
//        req.scope = "snsapi_userinfo"; // 只能填 snsapi_userinfo
//        req.state = "wechat_sdk_demo_test";
//        api.sendReq(req);

//        WxsdkUtil.gotoWxlogin(getApplicationContext());

    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
        switch (req.getType()) {
            case ConstantsAPI.COMMAND_GETMESSAGE_FROM_WX:
                break;
            case ConstantsAPI.COMMAND_SHOWMESSAGE_FROM_WX:
                break;
            case ConstantsAPI.COMMAND_PAY_BY_WX:
                break;
            default:
                break;
        }
        LogMds.w("cdz","req===================="+req.getType());
    }

    @Override
    public void onResp(BaseResp resp) {
        int errCode = resp.errCode;
        LogMds.d(TAG, "支付结果回调：" + errCode);
        // 支付结果处理（0=成功，-1=错误，-2=用户取消）
        int type = resp.getType();
        if (type == ConstantsAPI.COMMAND_PAY_BY_WX) { // 5 表示支付类型回调
            switch (errCode) {
                case 0:
                    // 支付成功，可跳转到成功页面
                    finish();
                    break;
                case -1:
                    // 支付失败
                    finish();
                    break;
                case -2:
                    // 用户取消支付
                    finish();
                    break;
            }
            EventBus.getDefault().post(new WxPayEvent(WxPayEvent.GET_WX_PAY_RESULT,errCode));
        }
    }
}